package com.dao;

import com.alibaba.fastjson.JSONObject;
import com.entity.Node;
import netscape.javascript.JSObject;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

public class ViewTreeManager {
    private static Resource resource;
    private static String rootFolderPath;
    private static Node root;
    static {
        //加载资源
        resource = new ClassPathResource("userdata");
        try {
            rootFolderPath=resource.getFile().getPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //反序列化
        deserialize();
        //刷新一下path
        root.refreshPath();
    }
    //保存
    public static void serialize(){
        JSONObject children=root.getNode().getJSONObject("children");
        BufferedWriter bw= null;
        Iterator<Map.Entry<String, Object>> it = children.entrySet().iterator();
        try {
            while (it.hasNext()){
                Map.Entry<String, Object> next = it.next();
                JSONObject child = (JSONObject)next.getValue();
                String fName=child.getString("name");
                //没有的文件会新建，所以有新的视图只需改变root，再调用serialize方法即可
                bw = new BufferedWriter(new FileWriter(rootFolderPath+"/"+fName));
                bw.write(child.toJSONString());
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //解析
    public static void deserialize(){
        //新建root
        JSONObject JRoot=new JSONObject();
        JRoot.put("path","");
        root=new Node(JRoot);
        //把资源中的view加载到root
        File f=new File(rootFolderPath);
        Iterator<File> it= Arrays.stream(f.listFiles()).iterator();
        BufferedReader br = null;
        try {
            while (it.hasNext()){
                File file=it.next();
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                JSONObject jView = JSONObject.parseObject(br.readLine());
                //视图的id在每次服务器重启时都会重新分配，视图id不会被持久化。
                jView.put("name",file.getName());
                root.appendChild(jView);
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void newView(String name){
        name=name+".json";
        JSONObject JRoot=new JSONObject();
        JRoot.put("name",name);
        JRoot.put("type","view");
        root.appendChild(JRoot);
        serialize();
    }
    public static void deleteView(String id){
        //判断id长度是否正常，是否指向视图
        /*...*/
        //保存一下，万一有改动呢
        serialize();
        //先删除文件，再删json
        JSONObject target = root.getChildNodeByPath(id);
        File f = new File(target.getString("name"));
        f.delete();
        root.deleteChild(id);
    }
    public static void refreshPath(){
        root.refreshPath();
        System.out.println(root.getNode());
    }
    public static JSONObject getRoot(){
        JSONObject rootClone=(JSONObject) root.getNode().clone();
        return rootClone;
    }
    public static JSONObject getTagUnion(String path){
        return root.getTagUnion(path);
    }
}
